function oil = osl_setup_oil(S)

% oil = osl_setup_oil_for_ica(S)
%
% Creates an OAT (OHBA's easy Analysis Tool) struct with all the appropriate settings which can then be passed to
% oil=osl_run_ica(oil); to use the IC pipeline.
%
% INPUTS:
% 
% General Parameters - stored in oil.
% S.oil_name - the string name of the OAT (e.g. S.oil_name = '/home/myusername/4-8Hz_espm8_ctf_2back';).
% S.paradigm - the typoe of data being used. (S.paradigm = 'task'; OR S.paradigm = 'rest'; 
% S.to_do    - sets the oil.to_do (e.g. S.to_do = [1 1 1 1 0 0]).
%
% Source Reconstruction Parameters - stored in oil.source_recon
% S.triggers   - list of triggers (conditions) to include in the analysis (e.g. S.triggers={{''Motorbike''},{''Neutral face''}}').
% S.spm_files  - list of SPM MEEG object file paths to run the analysis on e.g. S.spm_files{1}='subject1'; S.spm_files{2}='subject2'
% S.time_range - Time range (from to) within trial, in secs, need to all be the same duration and one for each trigger. Set to '' for continuous data.    
% S.freq_range -  Frequency range (from to) in Hz
% S.pca_dim    - the estimated dimensionality of the data. For non-maxfiltered data set to 0.
%
% Enveloping Parameters - stored in oil.enveloping
% S.hilbert_downsampling - the downsampling window length is seconds (default 1s).
% S.overlap              - overlap between down-sampling windows as fraction (default 0).
% S.spatial_downsampling - the spatial resampling after enveloping (default 8mm).
% S.spatial_smoothing    - FWHM of Gaussian Kernal for spatial smoothing (default 4mm).
% S.timewindow           - the subset time window for the enveloping (to minimise edge effects).
%
% Concatenation Parameters - stored in oil.concat_subs
% S.ica_subs2use - the subset of subjects passed into the beamformer (default is to use all subjects).
% S.normalise_subjects - global variance normalisation of each subject prior to concatenation (default is 0).
%
% ICA Parameters - stored in oil.ica
% S.num_ics - the number of components sought
% S.last_eig - the dimension of the PCA prior to ICA (default equal to num_ics).
% S.temp_or_spat - select either 'temporal' or 'spatial' ICA. (default is temporal).
% S.icasso_its - number of ICASSO iterations (default is 0).
%
% First Level ICA Statistics - stored in oil.ica_first_level
% S.design_matrix_summary - this is a parsimonious description of the design matrix.
% S.contrast - E.g. Two different contrasts, for a design matrix with 3 regressors: S.contrast{1}=[1 0 0]'; S.contrast{2}=[0 1 -1]'; 
%
% Group Level ICA Statistics - stored in oil.ica_group_level
% S.group_design_matrix - usually a unity vector whose length is equal to the number of subjects (to give group mean).
% S.group_contrast - usually [1].
%
% HL 15.08.12

%%%%%%%%%%%%%%%%%%%
% General Settings
try, oil.name=[S.oil_name '.oil']; catch, oil.name=[S.spm_files{1} '.oil']; end
try, oil.to_do=S.to_do; catch, oil.to_do=[1 1 1 1 0 0]; end
try, oil.paradigm=S.paradigm; catch, error('User must specify the paradigm type: S.paradigm, It can be either rest or task.'); end

%%%%%%%%%%%%%%%%%%%
% source recon settings
oil.source_recon=[]; 

% required settings:
try, oil.source_recon.trigger=S.trigger; catch, error('S.trigger not specified, list of triggers (conditions) to include in the analysis, e.g. trigger={{''Motorbike''},{''Neutral face''}}'); end;
try, oil.source_recon.D=S.D; catch, error('S.D not specified'); end; % list of SPM MEEG object file paths to run the analysis on
try, oil.source_recon.time_range = S.time_range; catch, error('S.time_range not specified'); end; % Time range (from to) within trial, in secs, need to all be the same duration and one for each trigger    
try, oil.source_recon.freq_range = S.freq_range; catch, error('S.freq_range not specified'); end; % Frequency range (from to) in Hz

% optional settings:
try, oil.source_recon.recon_method=S.recon_method; catch, oil.source_recon.recon_method='beamform'; end; % can be 'beamform' or 'none' (for a sensor space analysis)
try, oil.source_recon.analysis_name=S.analysis_name; catch, oil.source_recon.analysis_name='oil'; end;
oil.source_recon.name='source_recon';
try, oil.source_recon.modalities=S.modalities; catch, warning('Using default Neuromag modalities'); oil.source_recon.modalities={'MEGPLANAR', 'MEGMAG'}; end; % modalities to include
try, oil.source_recon.pca_dim=S.pca_dim; catch, oil.source_recon.pca_dim=60; end; % rank to be use for the pca dimensionality reduction. If pca_dim=-1 then spm_pca_order is used to estimate pca_dim. If oil.source_recon.do_reduce_pca=1 then this is used prior to any source recon, if oil.source_recon.do_reduce_pca=0 then this is only used to determine the rank of the beamformer data covariance
try, oil.source_recon.regpc=S.regpc; catch, oil.source_recon.regpc=400; end; % data covariance regularisation
try, oil.source_recon.gridstep=S.gridstep; catch, oil.source_recon.gridstep=7; end; % space between dipoles in beamformer grid in mm 
    
try, oil.source_recon.mask_fname=S.source_recon_mask_fname; catch, end;

try, oil.source_recon.useheadshape = S.useheadshape; catch, oil.source_recon.useheadshape=1; end 
try, oil.source_recon.forward_meg = S.forward_meg; catch, oil.source_recon.forward_meg='MEG Local Spheres'; end % MEG head forward model set to 'Single Shell' or 'MEG Local Spheres'
try, oil.source_recon.beamformer_fn = S.beamformer_fn; catch, oil.source_recon.beamformer_fn='osl_beamformer_spm_batch';end;
try, oil.source_recon.mri = S.mri; catch, for i=1:length(oil.source_recon.D), oil.source_recon.mri{i}=''; end; end 
try, oil.source_recon.fid_label = S.fid_label; catch, warning('Using default Neuromag fid labels'); oil.source_recon.fid_label.nasion='Nasion'; oil.source_recon.fid_label.lpa='LPA'; oil.source_recon.fid_label.rpa='RPA'; end; % To see what these should be look at: D.fiducials; fidnew.fid.label
%%%%%%%%%%%%%%%%%%%
% Enveloping Settings
oil.enveloping=[];
try oil.enveloping.window_length=S.hilbert_downsampling; catch, oil.enveloping.window_length=1;end;    
try oil.enveloping.gridstep=S.spatial_downsampling; catch, oil.enveloping.gridstep=8;end;
try oil.enveloping.ss=S.spatial_smoothing; catch, oil.enveloping.ss=4;end;
try oil.enveloping.overlap=S.window_overlap; catch, oil.enveloping.overlap=0;end;
try oil.enveloping.timewindow=S.envelope_timewindow; catch, oil.enveloping.timewindow='all';end;

%%%%%%%%%%%%%%%%%%%
% Concatenation Settings
oil.concat_subs=[];
try oil.concat_subs.subs2use=S.ica_subs2use; catch,oil.concat_subs.subs2use = 1:length(oil.source_recon.D);end;
try oil.concat_subs.normalise_subjects=S.normalise_subjects; catch,oil.concat_subs.normalise_subjects=0;end;

%%%%%%%%%%%%%%%%%%%
% ICA Settings
oil.ica=[];
try oil.ica.temp_or_spat=S.temp_or_spat; catch, oil.ica.temp_or_spat='temporal';end;
try oil.ica.use_gm_mask=S.use_gm_mask; catch, oil.ica.use_gm_mask=0;end;
try oil.ica.num_ics=S.num_ics; catch, oil.ica.num_ics=25;end;
try oil.ica.icasso_its=S.icasso_its; catch, oil.ica.icasso_its=0;end;
try oil.ica.last_eig=S.last_eig; catch, oil.ica.last_eig=oil.ica.num_ics;end;
try oil.ica.other_oils=S.other_oils; catch, end;
try oil.ica.nuisance_oil_names=S.nuisance_oil_names; catch, end;
try oil.ica.normalise_vox=S.normalise_vox; catch, oil.ica.normalise_vox=0; end;

%%%%%%%%%%%%%%%%%%%
% First Level Stats Settings
oil.ica_first_level=[];
% required settings:
try oil.ica_first_level.design_matrix_summary=S.design_matrix_summary; catch, error('S.design_matrix_summary not specified'); end;
try oil.ica_first_level.contrast=S.contrast; catch, error('S.contrast not specified'); end;
oil.ica_first_level.name='ica_first_level';
try, oil.ica_first_level.time_range=S.ica_first_level_time_range; catch, oil.ica_first_level.time_range=oil.source_recon.time_range; end;
try, oil.ica_first_level.use_robust_glm=S.use_robust_glm; catch, oil.ica_first_level.use_robust_glm=0; end; % do robust GLM (uses bisquare via Matlab robustfit fn)
try, oil.ica_first_level.cope_type=S.cope_type; catch, oil.ica_first_level.cope_type='cope'; end % cope type to input to group GLM (from first level), set to 'coape', 'cope', or 'acope'

%%%%%%%%%%%%%%%%%%%
% Group Level Stats Settings

oil.ica_group_level=[];
try, oil.ica_group_level.group_design_matrix=S.group_design_matrix; catch, oil.ica_group_level.group_design_matrix=ones(1,length(oil.source_recon.D)); end;
try, oil.ica_group_level.group_contrast=S.group_contrast; catch, oil.ica_group_level.group_contrast{1}=[1]; end;

